Imports VSUtility
Imports System.Net
Imports System.IO
Imports System.Threading

Module ModMain

  Public PlayerWhite As String
  Public PlayerBlack As String
  Public frm As frmMain
  Public AppConfig As clsXML  'File for XML Configuration
  Public frmMsg As frmMessage
  Public RemoveMessageBox As Boolean = True
  Public BaseURL As String = "http://games.vikasumit.com/chessv2/"
  'Public BaseURL As String = "http://localhost:780/vikasumit/games/chessv2/"
  Public OnlineLink As String
  Public OfflineLink As String
  Public HTTPReferrer As String
  Private AutoThread As Thread
  Public MyPath As String

  'Main Start Point of Application
  Public Sub Main()
    AppConfig = New clsXML
    frmMsg = New frmMessage

    MyPath = Application.StartupPath

    OnlineLink = BaseURL & "makeonline.php"
    OfflineLink = BaseURL & "makeoffline.php"

    HTTPReferrer = "VS ChessMania V" & Application.ProductVersion & _
    "( " & Environment.OSVersion.ToString & " with .Net Version " & Environment.Version.ToString & " )"

    DeclareParameters()

    AppConfig.ConfigurationFor = "Vs ChessMania"
    AppConfig.ConfigurationFile = MyPath & "\config.xml"

    If Not File.Exists(AppConfig.ConfigurationFile) Then
      If Not AppConfig.CreateBlankConfigurationFile() Then
        MsgBox("Unable to read or create new Configuration file. Check Configuration file or permission to application before you can play game.")
        Exit Sub
      End If
    End If

    'Code on 23rd Aug 2006 for AutoUpdate
    Try
      Dim AutoUpdateCheck As String = AppConfig.GetValue("Settings", "AutoUpdateEnable")
      If AutoUpdateCheck = "1" Then
        AutoThread = New Thread(AddressOf VersionCheck)
        AutoThread.Start()
      End If
    Catch
    End Try
    'AutoUpdateEnd

    frm = New frmMain
    frm.Focus()
    Application.Run(frm)

  End Sub

  Public Sub EndApplication()
    End
  End Sub

  'Code on 23rd Aug 2006 for AutoUpdate
  Private Sub VersionCheck()
    Try
      Dim OnlineVersion As String = BaseURL & "VERSION"
      Dim re As HttpWebRequest = CType(WebRequest.Create(OnlineVersion), HttpWebRequest)

      re.UserAgent = HTTPReferrer
      Dim res As HttpWebResponse = CType(re.GetResponse(), HttpWebResponse)
      Dim receiveStream As Stream = res.GetResponseStream()
      Dim VersionCheck As Integer = 0
      ' Pipes the stream to a higher level stream reader with the required encoding format. 

      Dim sr As New StreamReader(receiveStream)
      Dim str As String = ""

      Dim i As Integer = 0
      While sr.Peek > -1
        str = sr.ReadLine
        'str has complete line in it.
      End While
      Dim ver As String()

      ver = Split(str, ".")
      If ver.GetLength(0) = 3 Then
        Dim myVersion As String = Application.ProductVersion
        Dim myVersions() As String = Split(myVersion, ".")
        If myVersions(0) = ver(0) Then
          If myVersions(1) = ver(1) Then
            If myVersions(2) >= ver(2) Then
              VersionCheck = 1
            Else
              VersionCheck = -1
            End If
          ElseIf myVersions(1) > ver(1) Then
            VersionCheck = 1
          Else
            VersionCheck = -1
          End If
        ElseIf myVersions(0) > ver(0) Then
          VersionCheck = 1
        Else
          VersionCheck = -1
        End If
      Else
        VersionCheck = 0
      End If

      If VersionCheck = 1 Then
        'Do Nothing
      ElseIf VersionCheck = 0 Then
        MsgBox("Unable to detect online version")
      ElseIf VersionCheck = -1 Then
        MsgBox("A New Version is available for download")
      End If
      sr.Close()
      res.Close()
    Catch ex As Exception
      'No need to show the exception
    Finally
      AutoThread.Abort()
      AutoThread = Nothing
    End Try
  End Sub
End Module
